/**
 * 生成 table
 * 例子：
  function abc (params) {
    if (params.abc && params.abc.dd) {
      return params.abc.dd
    }
  }
  function aa(params) {
    let xx = {
      '@item': ....,
      '@list: ...
    }
    return xx
  }
  const xx = tableList({
    headData: [
      { title: '尽调报告编号', key: 'businessKey'},
      { title: '小微企业', key: 'documentName'},
      { title: '发起人', key: 'cruser', formatter: abc},
      { title: '发起时间', key: 'crtime', formatter: function(params) {
        return params.status + params.documentName
      }},
    ],
    data: data.data.list,
    tableClass: 'auto mb-default',
    autoIdx: {
      show: true
    }
  })
 */

const Pager = require('../aotoo/core/index')
let lib = Pager.lib


module.exports = function (opts={}) {
  let dft = {
    autoIdx: {    //是否需要自动序号
      show: false,
      text: '序号',
      start: 0,      //指定开始的值，默认从0开始
    },
    unit: 'rpx',      //单位
    autoWidth: false
  }
  opts.autoIdx = Object.assign({}, dft.autoIdx, opts.autoIdx)
  const newCfg = Object.assign({}, dft, opts)
  let countSum = 0

  // autoIdx 是否生成序号列
  if (newCfg.autoIdx && newCfg.autoIdx.show) {
    newCfg.headData.unshift({title: newCfg.autoIdx.text, key: 'ssindex'})
    newCfg.data = newCfg.data.map(item => {
      item.ssindex = newCfg.autoIdx.start
      return item
    })
  }
  let _head_data = newCfg.headData.map((item, ii) => {
    item.width =  !newCfg.autoWidth ? item.width || '140' : ''
    countSum += parseInt(item.width)
    // let _itemStyle = (item.width ? 'width: '+item.width + newCfg.unit : '')  + (item.sticky ? '; position: sticky; left: 0' : '')
    let _itemStyle = (item.width ? 'width: '+item.width + newCfg.unit : '')
    return { title: item.title, itemClass: `ss-table-td ${item.sticky ? ' item-sticky' : ''} ${item.itemClass}`, itemStyle: _itemStyle}
  })
  // const header = {dot: _head_data, itemClass: 'ss-table-tthead'}
  const header = {
    // '@list': {
    //   data: _head_data,
    //   listClass: 'ss-table-tr'
    // },
    dot: _head_data,
    itemClass: 'ss-table-tr'
  }
  if (newCfg.unit === '%') {
    countSum = 100
  }

  const _itemStyle = countSum > 0 && {
    itemStyle: '--width: '+ countSum.toString() + newCfg.unit
  }
  return {
    id: newCfg.$$id || lib.suid('table_'),
    '@list': {
      data: [],
      listClass: 'ss-table-sx',
      header: !newCfg.isHideHeader ? header : null,
      ready() {
        this.update(this.adapter(newCfg.data))
      },
      hasItemClick(e, params, inst) {
        const {value, idx, key} = params
        const instTd = this.find({id: key+'_'+idx})
        return newCfg.onItemClick.call(instTd, params)
      },
      adapter(data) {
        return data.map( (item, ii) => {
          let res = newCfg.autoWidth ? {
            dot: this.bdlist(item, ii),
            itemClass: 'ss-table-tr',
          } : {
            '@list': {
              data: this.bdlist(item, ii),
              listClass: 'ss-table-tr'
            }
          }
          return {
            // ...res
            dot: this.bdlist(item, ii),
            itemClass: 'ss-table-tr',
          }
        })
      },
      bdlist(data, ii) {
        let output = []
        for (let i =0; i < newCfg.headData.length; i++){
          // let _itemStyle = (newCfg.headData[i].width ? 'width: ' + newCfg.headData[i].width + newCfg.unit : '') + (newCfg.headData[i].sticky ? '; position: sticky; left: 0' : '')
          let _itemStyle = (newCfg.headData[i].width ? 'width: ' + newCfg.headData[i].width + newCfg.unit : '')
          let _title = null
          if ( newCfg.headData[i].key === 'ssindex') {
            _title = String((ii + data[newCfg.headData[i].key]))
          }
          else {
            if (typeof newCfg.headData[i].formatter === 'function') {
              _title = newCfg.headData[i].formatter && newCfg.headData[i].formatter.call(this, data)
            }
            else {
              _title = data[newCfg.headData[i].key]
            }
          }
          output.push(
            {
              title: _title || '-',
              itemClass: `ss-table-td ${newCfg.headData[i].sticky ? ' item-sticky' : ''} ${newCfg.headData[i].itemClass}`,
              id: newCfg.headData[i].key+'_'+ii,
              aim: newCfg.headData[i].hasClick ? 'hasItemClick?value='+_title+'&idx='+ii+'&key='+newCfg.headData[i].key : '',
              // aim: newCfg.headData[i].hasClick ? newCfg.headData[i].hasClick.call(this, data, ii) : '',
              itemStyle: _itemStyle
            }
          )
        }
        return output
      },
      
    },
    itemClass: 'ss-table ' + newCfg.tableClass,
    ..._itemStyle,
  }
}